﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;



namespace Quản_Lý_Thư_Viện
{
    public partial class frmLoaiTacGia : Form
    {
        public frmLoaiTacGia()
        {
            InitializeComponent();
        }
        TacgiaControl control = new TacgiaControl();
        bool kt = false;
        bool bInserted = false;
        public static string timtg;
              
        private void frmLoaiTacGia_Load(object sender, EventArgs e)
        {
            dgvTacGia.DataSource = control.LayDSTacgia();
        }
        private void Laythongtintacgia(ref tacgiainfo info)
        {
            info.Matg = txtMaTacGia.Text;
            info.Tentg = txtTenTacGia.Text;
        }

        private void dgvTacGia_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvTacGia.RowCount > 0)
            {
                txtMaTacGia.Text = dgvTacGia.CurrentRow.Cells["MATG"].Value.ToString();
                txtTenTacGia.Text = dgvTacGia.CurrentRow.Cells["TENTG"].Value.ToString();
            }
        }
            
        private void ShowControl(bool bShow)
        {
            //txtMaTacGia.Enabled = bInserted;
            txtTenTacGia.Enabled = bShow;
            btnLuu.Enabled = bShow;
        }
        private void Clear()
        {
            txtMaTacGia.Text = "";
            txtTenTacGia.Text = "";
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            bInserted = true;
            Clear();
            ShowControl(true);           
            btnThem.Enabled = false;
            btnSua.Enabled = false;
            int i = dgvTacGia.Rows.Count + 1;
            txtMaTacGia.Text = "TG" + i + "";
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            tacgiainfo tginfo = new tacgiainfo();
            Laythongtintacgia(ref tginfo);
            if ( txtTenTacGia.Text == "")
            {
                MessageBox.Show("Chưa Nhập Đầy Đủ Các Thành Phần Yêu Cầu !", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                if (bInserted)
                {

                    if (kiemtra(txtMaTacGia.Text.Trim()) == true)
                        MessageBox.Show("Mã Tác Gỉả Đã Tồn Tại !");
                    else
                    {
                        control.inserttacgia(tginfo);
                        MessageBox.Show("Thêm Tác Giả Thành Công ");
                        dgvTacGia.DataSource = control.LayDSTacgia();
                        bInserted = false;
                        ShowControl(false);
                        btnThem.Enabled = true;
                        btnSua.Enabled = true;
                    }
                }

                else
                {

                    Laythongtintacgia(ref tginfo);
                    control.updatetacgia(tginfo);
                    MessageBox.Show("Sửa Dữ Liệu Thành Công ");
                    dgvTacGia.DataSource = control.LayDSTacgia();
                    ShowControl(false);
                    btnThem.Enabled = true;
                    btnSua.Enabled = true;
                }
            }

        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            bInserted = false;
            ShowControl(true);           
            btnSua.Enabled = false;
            btnThem.Enabled = false;
            
        }
        private Boolean kiemtra(string ma)
        {

            for (int j = 0; j < dgvTacGia.RowCount; j++)
            {
                if (ma == dgvTacGia.Rows[j].Cells[0].Value.ToString())
                {
                    kt = true;
                    break;
                }
                else
                    kt = false;
            }
            return kt;
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            if (dgvTacGia.RowCount > 0)
            {
                txtMaTacGia.Text = dgvTacGia.CurrentRow.Cells["MATG"].Value.ToString();
                txtTenTacGia.Text = dgvTacGia.CurrentRow.Cells["TENTG"].Value.ToString();
            }
            bInserted = false;
            ShowControl(false);
            btnThem.Enabled = true;
            btnSua.Enabled = true;
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Escape))
            {
                this.Close();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            timtg = textBox3.Text;
            dgvTacGia.DataSource =  control.timtg();
        }      
       
    }
}
